package com.cai.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.cai.constant.MessageConstant;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.entity.Result;
import com.cai.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            org.springframework.security.core.userdetails.User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
    }
    //用户分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody com.cai.pojo.User user, Integer[] roleIds) {
        try {
            userService.add(user,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
        return new Result(true, "添加成功");
    }
    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
           userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除成功");
    }
    //根据id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            com.cai.pojo.User user = userService.findUserById(id);
            return new Result(true, "查询成功", user);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
    //根据角色id查询对应的权限id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> list = userService.findRoleIdsByUserId(id);
            return new Result(true, "查询成功", list);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
    //编辑修改角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.cai.pojo.User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
            return new Result(true,"编辑成功");//编辑成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
    }
}
